<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-switch
          v-model="formData.sex"
          active-text="男"
          inactive-text="女"
          :active-value="0"
          :inactive-value="1"
        />
      </el-form-item>
      <el-form-item label="广告图" prop="icon">
        <!-- 自己上传 action  http-request -->
        <!-- fileList 作用就是回显！ -->
        <el-upload
          action="#"
          :file-list="fileList"
          :http-request="httpRequest"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addAdvUserInfoApi, updateAdvUserInfoApi } from '@/api/adv_user'
import { UploadFile } from '@/api/user'
export default {
  name: 'AddFormRos',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        id: 0,
        username: '',
        mobile: '',
        sex: 0,
        password: '',
        icon: '',
        notes: null,
        birthday: null,
        address: null
      },
      // 表单验证规则
      formRules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [{ required: true, message: '请输入', trigger: 'blur' }],
        sex: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  methods: {
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateAdvUserInfoApi(this.formData)
        : await addAdvUserInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        id: 0,
        username: '',
        mobile: '',
        sex: 0,
        password: '',
        icon: '',
        notes: null,
        birthday: null,
        address: null
      }
      // 移除校验
      this.$refs.form.clearValidate()
      // 清除
      this.fileList = []
    },
    // 上传行为
    async httpRequest(result) {
      // 上传
      const fd = new FormData()
      fd.append('file', result.file)
      const res = await UploadFile(fd)
      // 服务器端存储地址放在表单字段里面
      // 思路1：放在表单里面
      this.formData.advimgsrc = res.savePath
      // 思路2：放在fileList里面去
    }
  }
}
</script>
<style scoped></style>
